﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using Gencode.PrsCommon.Data.Plan;
using Gencode.PrsCommon.App;

namespace Gencode.PrsCommon.ControlData.GridControl
{
	/// <summary>
	/// DO NOT REMOVE THIS CLASS
	/// PlanGridGenerator is used in PrsWeb
	/// This class can't be removed before PrsWeb is not changed
	/// </summary>
	public class PlanGridGenerator
	{
		PlanNameGrid nameGrid;
		PlanDienstGrid dienstGrid;
		PlanSaldoGrid saldoGrid;

		#region Properties
		public DataTable NameTable
		{
			get
			{
				if (nameGrid != null)
					return nameGrid.Table;
				return new DataTable();
			}
		}

		public DataTable DienstTable
		{
			get
			{
				if (dienstGrid != null)
					return dienstGrid.Table;
				return new DataTable();
			}
		}

		public DataTable SaldoTabel
		{
			get
			{
				if (saldoGrid != null)
					return saldoGrid.Table;
				return new DataTable();
			}
		}
		public DataView PlanNames
		{
			get;
			private set;
		}
		#endregion

		public PlanGridGenerator()
		{
			nameGrid = new PlanNameGrid();
			dienstGrid = new PlanDienstGrid();
			saldoGrid = new PlanSaldoGrid();
		}

		public void InitGrids(int planID)
		{
			PlanNames = PlanData.GetPersonenInPlan(planID);

            int cnt = 0;
			foreach (DataRowView datarow in PlanNames)
			{
                AddRow(datarow, planID);
                cnt++;
			}
		}

		private void AddRow(DataRowView row, int planID)
		{
			int prsschemaid = int.Parse(row[5].ToString());
			int prsid = int.Parse(row[0].ToString());

			string datumVon = DateUtil.ConvertSqlToDate(row[6].ToString());
			int month = int.Parse(DateUtil.GetMonth(datumVon));
			int year = int.Parse(DateUtil.GetYear(datumVon));
			int tageImMonat = DateTime.DaysInMonth(year, month);

			DataView dv = PlanData.GetPlanRow(planID, prsschemaid, prsid);

			if (prsid == 0)//Abteilung
			{
				nameGrid.AddItem(row[1].ToString());
				dienstGrid.AddRow(tageImMonat);
				saldoGrid.AddItem();
			}
			else if (prsid < 0)//Lehre zeile
			{
				nameGrid.AddItem();
				dienstGrid.AddRow(tageImMonat);
				saldoGrid.AddItem();
			}
			else if (prsid > 0)//Person
			{
				nameGrid.AddItem(row[1].ToString() + " " + row[2].ToString());
				dienstGrid.AddRow(dv, tageImMonat);
				saldoGrid.AddItem(prsid, planID, int.Parse(row["abteilungid"].ToString()));
			}
		}

		
	}
}
